package com.joy.eduservice.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.joy.eduservice.client.VodClient;
import com.joy.eduservice.entity.EduVideo;
import com.joy.eduservice.mapper.EduVideoMapper;
import com.joy.eduservice.service.EduVideoService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 课程视频 服务实现类
 * </p>
 *
 * @author testjava
 * @since 2021-12-02
 */
@Service
public class EduVideoServiceImpl extends ServiceImpl<EduVideoMapper, EduVideo> implements EduVideoService {

    @Autowired
    private VodClient vodClient;

    //根据课程id删除小节
    //TODO 删除小结的时候，需要删除视频
    @Override
    public void removeVideoByCourseId(String id) {
        //1、根据课程id查询课程所有的视频id
        QueryWrapper<EduVideo> wrapperVideo = new QueryWrapper<>();
        wrapperVideo.eq("course_id", id);
        wrapperVideo.select("video_source_id");//查询指定的段
        List<EduVideo> eduVideos = baseMapper.selectList(wrapperVideo);
        //将所有的id取出来
        List<String> videoId = new ArrayList<String>();
        for(int i = 0;i<eduVideos.size();i++){
            EduVideo eduVideo = eduVideos.get(i);
            String videoSourceId = eduVideo.getVideoSourceId();
            if(!StringUtils.isEmpty(videoSourceId)){
                //放到集合里面
                videoId.add(videoSourceId);
            }
        }

        //根据多个视频id删除多个视频
        if (videoId.size()>0) {
            vodClient.deleteBatch(videoId);
        }

        //删除完视频，再删小结
        QueryWrapper<EduVideo> wrapper = new QueryWrapper<>();
        wrapper.eq("course_id",id);
        baseMapper.delete(wrapper);
    }
}
